package com.luocj.looook.activity;

import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;

import com.luocj.looook.R;
import com.luocj.looook.base.BaseActivity;
import com.luocj.looook.fragment.Fragment1;
import com.luocj.looook.fragment.Fragment2;
import com.luocj.looook.fragment.Fragment3;

import java.util.ArrayList;

import butterknife.BindView;

public class DarwerActivty extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener {
    @BindView(R.id.drawerlayout)
    DrawerLayout mDrawerLayout;
    @BindView(R.id.navigationview)
    NavigationView mNavigationView;
    private ArrayList<Fragment> fragments;
    private Fragment1 fragment1;
    private int index;
    private int currnetTabIndex;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_darwer;
    }

    @Override
    protected void initView() {
        initFragment();

        initNavigationView();
    }

    private void initNavigationView() {
        mNavigationView.setNavigationItemSelectedListener(this);
        View headerView = mNavigationView.getHeaderView(0);
        ImageView headView = headerView.findViewById(R.id.navigation_header_view);
        headView.setImageResource(R.mipmap.b1);

    }

    private void initFragment() {
        fragment1 = new Fragment1();
        Fragment2 fragment2 = new Fragment2();
        Fragment3 fragment3 = new Fragment3();
        fragments = new ArrayList<>();
        fragments.add(fragment1);
        fragments.add(fragment2);
        fragments.add(fragment3);

        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.container, fragment1)
                .show(fragment1).commit();
    }

    @Override
    protected void initData() {

    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        mDrawerLayout.closeDrawer(Gravity.START);
        switch (item.getItemId()) {
            case R.id.item_fragment1:
                changeFragment(item, 0);
                return true;
            case R.id.item_fragment2:
                changeFragment(item, 1);
                return true;
            case R.id.item_fragment3:
                changeFragment(item, 2);
                return true;
        }

        return false;

    }

    private void changeFragment(MenuItem item, int i) {
        index = i;
        switchFragment();
        item.setCheckable(true);
    }

    private void switchFragment() {
        FragmentTransaction transaction =
                getSupportFragmentManager().beginTransaction();
        transaction.hide(fragments.get(currnetTabIndex));

        if (!fragments.get(index).isAdded()) {
            transaction.add(R.id.container, fragments.get(index));
        }
        transaction.show(fragments.get(index)).commit();
        currnetTabIndex = index;
    }
}
